//
//  WJPublishLinksVC.m
//  WJBSApp
//
//  Created by dllo on 16/7/30.
//  Copyright © 2016年 王景伟. All rights reserved.
//
//  链接的页面

#import "WJPublishLinksVC.h"
#import "WJTextView.h"
#import "WJDataManager.h"
#import "SVProgressHUD.h"
#import "WJPublishModel.h"

@interface WJPublishLinksVC ()<UITextViewDelegate>

@property (nonatomic,strong) WJBaseView *touView;

@property (nonatomic,strong) UITextField *textField;

@property (nonatomic,retain) WJTextView *textView;

@property (nonatomic,strong) WJBaseButton *rightbutton;

@end

@implementation WJPublishLinksVC


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.textField becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createView];
    
    [self createTextView];
}

- (void)createView
{
    self.touView = [[WJBaseView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    [self.view addSubview:self.touView];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(100, 24, WIDTH - 200, 40)];
    lable.text = @"发表链接";
    lable.textAlignment = 1 ;
    [self.touView addSubview:lable];
    
    WJBaseButton *button = [WJBaseButton buttonWithTitle:nil frame:CGRectMake(20, 30, 50, 30) target:self action:@selector(puLeftAction)];
    button.backgroundColor = BSClearColor;
    [button setImage:[UIImage imageNamed:@"navigationButtonReturn.png"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"navigationButtonReturnClick.png"] forState:UIControlStateHighlighted];
    [self.touView addSubview:button];
    
    self.rightbutton = [WJBaseButton buttonWithTitle:@"发表" frame:CGRectMake(WIDTH - 70, 30, 50, 30) target:self action:@selector(puRightAction)];
    [ self.rightbutton setTitleColor:BSBlackColor forState:0];
    self.rightbutton.backgroundColor = BSClearColor;
    [self.touView addSubview: self.rightbutton];
    [self.touView layoutIfNeeded];
    
}

- (void)createTextView
{
    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 64, WIDTH - 20, 50)];
    [self.view addSubview:self.textField];
    self.textField.text = @"https://www.";
    
    self.textView = [[WJTextView alloc]initWithFrame:CGRectMake(10, 64 + 50, WIDTH - 20, 400 )];
    [self.view addSubview:self.textView];
    _textView.alwaysBounceVertical = YES;
    _textView.delegate = self;
    _textView.placeholder = @"描述";
    self.editing = YES;
    self.textView.userInteractionEnabled = YES;
    
}

- (void)puLeftAction
{
    [self.textField resignFirstResponder];
    [self.textView resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:nil];
}

//  点击发表进入本地
- (void)puRightAction
{
    [self.textView resignFirstResponder];

    if (self.textField.text) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"发表成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            NSTimeInterval time=[[NSDate date] timeIntervalSince1970];
            // 美国时间
            NSDate*detaildate=[NSDate dateWithTimeIntervalSince1970:time];
            
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
            
            dateFormatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";
            // 北京时间
            NSString*currentDateStr = [dateFormatter stringFromDate:detaildate];
            
            WJPublishModel *model = [[WJPublishModel alloc]init];
            model.type = @"links";
            model.links = self.textField.text;
            model.text = self.textView.text;
            model.nowTime = currentDateStr;
            [[WJDataManager shareFMDB]insertModel:model];

            [self dismissViewControllerAnimated:YES completion:nil];
            
        }];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:^{}];

    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"描述内容不可以为空" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
        [self.textView resignFirstResponder];
        }];
    }
    
}

#pragma mark -- UITextFieldDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //拖拽时 结束编辑 退出键盘
    [self.textView endEditing:YES];
    [UIView animateWithDuration:0.5 animations:^{
        self.textView.frame = CGRectMake(15, 64 + 50, WIDTH - 30, HEIGHT - 64 - 50);
    }];
}

- (void)textViewDidChange:(UITextView *)textView{
    //判读是否有文字  设置发表按钮的状态
    self.rightbutton.enabled = textView.hasText;
    if (textView.hasText) {
        
        self.textView.frame = CGRectMake(15, 64 + 50, WIDTH - 30,400);
        
    }
}








@end
